package com.example.imtoken.modules.main.wallet.manage.home

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.entitys.http.BalanceEntity
import com.example.imtoken.entitys.http.ResponseEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.WalletAddressSchema
import io.reactivex.schedulers.Schedulers
import java.util.*

class HomeWalletManageViewModel : BaseViewModel() {

    private var dataLiveData: MutableLiveData<MutableList<WalletAddressSchema>> = MutableLiveData()

    fun getDataLiveData(): MutableLiveData<MutableList<WalletAddressSchema>> {
        return dataLiveData
    }

    fun removeWalletAddress(position: Int) {
        val newData = ArrayList<WalletAddressSchema>()
        dataLiveData.value?.let {
            it.forEach { item ->
                newData.add(item.copy().apply {
                    this.id = item.id
                    this.fatherAddress = item.fatherAddress
                    this.isCurrency = item.isCurrency
                })
            }
            if (AppDataBase.instance.getWalletAddressDao()
                    .deleteWalletAddress(newData[position]) > 0
            ) {
                BusUtils.postSticky(
                    BusConfig.update_wallet_address_sticky_add_assets_activity,
                    newData[position].walletType
                )
                BusUtils.postSticky(BusConfig.update_wallet_address_sticky_assets_fragment)

                LogUtils.e("删除${newData[position].walletType}成功")
                newData.removeAt(position)
                dataLiveData.postValue(newData)
            } else {
                LogUtils.e("删除${newData[position].walletType}失败")
            }
        }
    }

    fun getWalletAddress() {
        val appLong = AppDataBase.instance.getAppLoginDao().getAppLogin()
        val walletChecked =
            AppDataBase.instance.getWalletCheckedDao().getCheckedWallet(appLong.walletAddress)

        val walletAddress = AppDataBase.instance.getWalletAddressDao().getAccountWalletAddress(walletChecked?.address)
        walletAddress?.add(
            0,
            WalletAddressSchema(
                walletChecked?.icon,
                walletChecked?.walletType,
                walletChecked?.name,
                walletChecked?.address,
                walletChecked?.description,
                walletChecked?.address
            ).apply {
                this.isCurrency = true
            }
        )

        RetrofitUtils.apiService?.balance(walletChecked?.walletType)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<BalanceEntity>>() {
                override fun onSuccess(value: ResponseEntity<BalanceEntity>) {
                    if (value.code == 0) {
                        if (value.data != null && walletAddress != null) {
                            for (address in walletAddress) {
                                for (v in value.data) {
                                    if (address.walletType == v.coin) {
                                        address.amount = v.amount
                                        break
                                    }
                                }
                            }
                        }
                        dataLiveData.postValue(walletAddress)
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {
                    LogUtils.e(e.message)
                }

            })
    }

}